package ru.averta.notes.dao.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ru.averta.notes.dao.TagConnectorDao;
import ru.averta.notes.domain.Note;
import ru.averta.notes.domain.Tag;
import ru.averta.notes.domain.TagConnector;
import ru.averta.notes.domain.flags.ITagable;
import ru.averta.notes.other.UtilsCommon;

@Repository
public class TagConnectorDaoImpl extends GenericDaoHibernateImpl<TagConnector, Long> implements TagConnectorDao {

	public Collection<TagConnector> findByObject(ITagable object) {
		
		String objectClass = UtilsCommon.getClassName(object);
		Long objectId = object.getId();
		
		return createCriteria()
		.add(Restrictions.eq("objectClass", objectClass))
		.add(Restrictions.eq("objectId", objectId))
		.list();
	}

	public TagConnector findByObjectAndTag(ITagable object, Tag tag) {
		TagConnector connector = new TagConnector(object, tag);
		return (TagConnector) createCriteria().add(Example.create(connector)).uniqueResult();
	}

}
